<template>
  <a-modal
    :open="open"
    width="700px"
    :title="rowData?.lsh ? '修改出库单' : '新增出库单'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form class="form-style-flex" name="validate_other" :model="formState">
      <a-form-item label="出库仓库" class="w-1/2" v-bind="validateInfos.outStoreName">
        <SelectWarehouse v-model:value="formState.outStoreName" @select="onSelectWarehouse" />
      </a-form-item>
      <a-form-item class="w-1/2 pl-5">
        <div>制单人：{{ empName }}</div>
      </a-form-item>
      <a-form-item label="去向" class="w-full" v-bind="validateInfos.destintation">
        <a-input v-model:value="formState.destintation" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full mb-3">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SelectWarehouse from '@/components/SelectWarehouse'

import useUserStore from '@/store/user'

const { empName } = useUserStore()
const emit = defineEmits(['update:open', 'update:value', 'success'])

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  outStoreName: '',
  destintation: ''
})
const rulesState = ref({
  outStoreName: [{ required: true, message: '请选择出库仓库' }],
  destintation: [{ required: true, message: '请输入去向' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/bloodm/BldStoreOutRecord/doSaveBldStoreOutRecord', { ...formState.value })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onSelectWarehouse = (_, supplier) => {
  formState.value.outStoreCode = supplier?.ckid
  formState.value.outStoreName = supplier?.ckmc
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
    }
  }
)
</script>
<style lang="less" scoped></style>
